#include "gtest/gtest.h"
#include "Thread/TaskExecutor.h"
#include <gtest/gtest.h>
#include <atomic>
#include <chrono>
#include <thread>

using namespace toolkit;

// 用于模拟 TaskExecutor 的具体实现类
class MockTaskExecutor : public TaskExecutor {
public:
    MockTaskExecutor(uint64_t max_size = 32, uint64_t max_usec = 2 * 1000 * 1000)
        : TaskExecutor(max_size, max_usec) {}

    Task::Ptr async(TaskIn task, bool may_sync = true) override {
        auto cancelable = std::make_shared<TaskCancelableImp<void()>>(std::move(task));
        // 立即执行任务
        if (cancelable) {
            (*cancelable)();
        }
        return cancelable;
    }
};

// ThreadLoadCounter 测试夹具
class ThreadLoadCounterTest : public ::testing::Test {
protected:
    ThreadLoadCounter counter{10, 1000000};
};

// 测试 ThreadLoadCounter 的初始负载
TEST_F(ThreadLoadCounterTest, InitialLoad) {
    EXPECT_EQ(counter.load(), 0);
}

// 测试 ThreadLoadCounter 的睡眠和唤醒逻辑
TEST_F(ThreadLoadCounterTest, SleepAndWakeUp) {
    counter.startSleep();
    std::this_thread::sleep_for(std::chrono::milliseconds(100));
    counter.sleepWakeUp();

    std::this_thread::sleep_for(std::chrono::milliseconds(100));
    int load = counter.load();
    EXPECT_GT(load, 0);
    EXPECT_LE(load, 100);
}

// TaskCancelableImp 测试夹具
class TaskCancelableImpTest : public ::testing::Test {
protected:
    std::atomic<bool> taskExecuted{false};
    // 使用 std::make_shared 创建 TaskCancelableImp 实例
    TaskCancelableImp<void()>::Ptr task = std::make_shared<TaskCancelableImp<void()>>([this]() { taskExecuted = true; });
};

// 测试 TaskCancelableImp 的任务执行
TEST_F(TaskCancelableImpTest, TaskExecution) {
    (*task)();
    EXPECT_TRUE(taskExecuted);
}

// 测试 TaskCancelableImp 的任务取消
TEST_F(TaskCancelableImpTest, TaskCancellation) {
    task->cancel();
    (*task)();
    EXPECT_FALSE(taskExecuted);
}

// TaskExecutorInterface 测试夹具
class TaskExecutorInterfaceTest : public ::testing::Test {
protected:
    std::shared_ptr<MockTaskExecutor> executor = std::make_shared<MockTaskExecutor>();
};

// 测试 TaskExecutorInterface 的异步任务执行
TEST_F(TaskExecutorInterfaceTest, AsyncTaskExecution) {
    std::atomic<bool> taskExecuted{false};
    auto asyncTask = executor->async([&taskExecuted]() { taskExecuted = true; });
    EXPECT_TRUE(taskExecuted);
}

// 测试 TaskExecutorInterface 的同步任务执行
TEST_F(TaskExecutorInterfaceTest, SyncTaskExecution) {
    std::atomic<bool> taskExecuted{false};
    executor->sync([&taskExecuted]() { taskExecuted = true; });
    EXPECT_TRUE(taskExecuted);
}

// 派生类，用于暴露 addPoller 方法
class TestableTaskExecutorGetterImp : public TaskExecutorGetterImp {
public:
    using TaskExecutorGetterImp::addPoller;
};

// TaskExecutorGetterImp 测试夹具
class TaskExecutorGetterImpTest : public ::testing::Test {
protected:
    TestableTaskExecutorGetterImp getter;
    void SetUp() override {
        getter.addPoller("test", 2, 0, false, false);
    }
};

// 测试 TaskExecutorGetterImp 获取执行器
TEST_F(TaskExecutorGetterImpTest, GetExecutor) {
    auto executor = getter.getExecutor();
    EXPECT_NE(executor, nullptr);
}

// 测试 TaskExecutorGetterImp 获取执行器负载
TEST_F(TaskExecutorGetterImpTest, GetExecutorLoad) {
    auto loads = getter.getExecutorLoad();
    EXPECT_EQ(loads.size(), 2);
}

int main(int argc, char **argv) {
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}